<!-- 用户登录界面 -->
<template>
  <div id="login">
    <div class="bg"></div>
    <el-row class="main-container">
      <el-col :lg="8" :xs="16" :md="10" :span="10">
        <div class="top">
          <i class="iconfont icon-kaoshi"></i><span class="title">东软试题库管理系统</span>
        </div>
        <div class="bottom">
          <div class="container">
            <p class="title">用户注册</p>

            <el-radio-group v-model="role">
              <el-radio :label="2">学生</el-radio>
              <el-radio :label="1">教师</el-radio>
            </el-radio-group>


            <el-form :label-position="labelPosition" label-width="80px" :model="studentRegisterInformation" v-if="role==2">
              <el-form-item label="昵称">
                <el-input v-model="studentRegisterInformation.studentNickname" placeholder="请输入昵称:必填"></el-input>
              </el-form-item>
              <el-form-item label="姓名">
                <el-input v-model="studentRegisterInformation.studentName" placeholder="请输入姓名:必填"></el-input>
              </el-form-item>
              <el-form-item label="密码">
                <el-input v-model="studentRegisterInformation.studentPwd" placeholder="密码超过5位,且必须包含数字和大小写字母" type='password'></el-input>
              </el-form-item>
              <el-form-item label="专业">
                <el-input v-model="studentRegisterInformation.studentDiscipline" placeholder="请输入专业"></el-input>
              </el-form-item>
            </el-form>


            <el-form :label-position="labelPosition" label-width="80px" :model="teacherRegisterInformation" v-if="role==1">
              <el-form-item label="昵称">
                <el-input v-model="teacherRegisterInformation.teacherNickname" placeholder="请输入昵称:必填"></el-input>
              </el-form-item>
              <el-form-item label="姓名">
                <el-input v-model="teacherRegisterInformation.teacherName" placeholder="请输入姓名:必填"></el-input>
              </el-form-item>
              <el-form-item label="密码">
                <el-input v-model="teacherRegisterInformation.teacherPwd" placeholder="密码超过5位,且必须包含数字和大小写字母" type='password'></el-input>
              </el-form-item>
              <el-form-item label="专业">
                <el-input v-model="teacherRegisterInformation.teacherDiscipline" placeholder="请输入专业"></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="teacherRegisterInformation.teacherMail" placeholder="请输入邮箱"></el-input>
              </el-form-item>
            </el-form>

            <div class="submit">
              <el-button type="primary" class="row-login" @click="login()">注册</el-button>
            </div>
            <div class="options">
              <div class="register">
                <span>已有账号？</span>
                <span><router-link :to="{path:'/'}">去登录</router-link></span>
              </div>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row class="footer">
      <el-col>
        <p class="msg2"> ©2020 东软实训第五组保留所有权利</p>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import store from '@/vuex/store'
import {mapState} from 'vuex'

//发送请求
export default {
name: "register",
  data() {
    return {
      labelPosition: 'left',
      errorMessage:"",
      error:0,
      role:2,
      studentRegisterInformation: {
        studentNickname:"",
        studentName:"",
        studentPwd:"",
        studentDiscipline:""
      },
      teacherRegisterInformation: {
        teacherNickname:"",
        teacherName:"",
        teacherPwd:"",
        teacherDiscipline:"",
        teacherMail:""
      }
    }
  },
  methods: {
    //用户登录请求后台处理
    login() {
      console.log("注册操作执行-------");
      this.errorMessage="";
      this.error=0;
      if(this.role==2)
      {
        if(this.studentRegisterInformation.studentNickname=="") //错误提示
        {
          this.errorMessage+="昵称不能为空   ";
          this.error=1;
        }
        if(this.studentRegisterInformation.studentName=="")
        {
          this.errorMessage+="姓名不能为空   ";
          this.error=1;
        }
        if(! /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{5,}$/.test(this.studentRegisterInformation.studentPwd) )
        {
          this.errorMessage+="密码不符合规范   ";
          this.error=1;
        }
        if(this.error==1)
        {
          this.$message({
            showClose: true,
            type: 'error',
            message: this.errorMessage
          })
        }
        else
        {
          this.$axios({
            url: "/api/student/register",
            method: 'post',
            data: {
              ...this.studentRegisterInformation
            }
          }).then(res=>{
            let resData = res.message
            this.$message({
              showClose: true,
              type: 'success',
              message: "注册成功"
            })
            this.$cookies.set("ctype", this.role)
            this.$cookies.set("cusername", this.studentRegisterInformation.studentNickname)
            this.$cookies.set("cpwd", this.studentRegisterInformation.studentPwd)
            this.$router.push({path: '/index' }) //跳转到首页
          }).catch(reason => {
            this.$message({
              showClose: true,
              type: 'error',
              message: "昵称已存在"
            })
          })
        }
      }
      else
      {
        if(this.teacherRegisterInformation.teacherNickname=="") //错误提示
        {
          this.errorMessage+="昵称不能为空   ";
          this.error=1;
        }
        if(this.teacherRegisterInformation.teacherName=="")
        {
          this.errorMessage+="姓名不能为空   ";
          this.error=1;
        }
        if(! /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{5,}$/.test(this.teacherRegisterInformation.teacherPwd) )
        {
          this.errorMessage+="密码不符合规范   ";
          this.error=1;
        }
        if(this.error==1)
        {
          this.$message({
            showClose: true,
            type: 'error',
            message: this.errorMessage
          })
        }
        else
        {
          this.$axios({
            url: "/api/teacher/register",
            method: 'post',
            data: {
              ...this.teacherRegisterInformation
            }
          }).then(res=>{
            let resData = res.message
            this.$message({
              showClose: true,
              type: 'success',
              message: "注册成功"
            })
            this.$cookies.set("ctype", this.role)
            this.$cookies.set("cusername", this.teacherRegisterInformation.teacherNickname)
            this.$cookies.set("cpwd", this.teacherRegisterInformation.teacherPwd)
            this.$router.push({path: '/' }) //跳转到首页
          }).catch(reason => {
            this.$message({
              showClose: true,
              type: 'error',
              message: "昵称已存在"
            })
          })
        }
      }
    }
  },
  computed: mapState(["userInfo"]),
  mounted() {
  }
}
</script>

<style lang="scss" scoped>
.remind {
  border-radius: 4px;
  padding: 10px 20px;
  display: flex;
  position: fixed;
  right: 20px;
  bottom: 50%;
  flex-direction: column;
  color: #606266;
  background-color: #fff;
  border-left: 4px solid #409eff;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
.container {
  margin-bottom: 32px;
}
.container .el-radio-group {
  margin: 30px 0px;
}
a:link {
  color:#ff962a;
  text-decoration:none;
}
#login {
  font-size: 14px;
  color: #000;
  background-color: #fff;
}
#login .bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  overflow-y: auto;
  height: 100%;
  background: url('../../assets/img/loginbg.png')center top / cover no-repeat;
  background-color: #fff !important;
}
#login .main-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
#login .main-container .top {
  margin-top: 100px;
  font-size: 30px;
  color: #ff962a;
  display: flex;
  justify-content: center;
}
#login .top .icon-kaoshi {
  font-size: 80px;
}
#login .top .title {
  margin-top: 20px;
}
#login .bottom {
  display:flex;
  justify-content: center;
  background-color:#fff;
  border-radius: 5px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
#login .bottom .title {
  text-align: center;
  font-size: 30px;
}
.bottom .container .title {
  margin: 30px 0px;;
}
.bottom .submit .row-login {
  width: 100%;
  background-color: #04468b;
  border-color: #04468b;
  margin: 20px 0px 10px 0px;
  padding: 15px 20px;
}
.bottom .submit {
  display: flex;
  justify-content: center;
}
.footer {
  margin-top: 50px;
  text-align: center;
}
.footer .msg1 {
  font-size: 18px;
  color: #fff;
  margin-bottom: 15px;
}
.footer .msg2 {
  font-size: 14px;
  color: #ff962a;
  margin-top: 50px;
}
.bottom .options {
  margin-bottom: 40px;
  color: #ff962a;
  display: flex;
  justify-content: space-between;
}
.bottom .options > a {
  color: #ff962a;
}
.bottom .options .register span:nth-child(1) {
  color: #8C8C8C;
}
</style>
